<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Modules_Core_Controllers_Hook extends Gio_Core_Controller
{
	
	/**
	 * Configure hook
	 * 
	 * @return void
	 */
	public function configAction() 
	{
		$this->setNoRender();
		$this->disableLayout();
		
		$request = $this->getRequest();
		$module  = $request->getPost('mod');
		
		if ('_' == $module) {
			/**
			 * We are saving config for global hook
			 */
			$module = null;
		}
		$json = new Services_JSON();
		$hook 	= $request->getPost('name');
		$params = $request->getPost('params');
		$params = $json->decode($params);
		Gio_Core_Hook_Config::saveParams($params, $hook, $module);

		$this->getResponse()->setBody('RESULT_OK');
	}
	public function installAction()
	{
		$this->setNoRender();
		$this->disableLayout();
		$response = 'RESULT_NOT_OK';
		
		$request = $this->getRequest();
		if (!$request->isPost()) {
			return;
		}
		
		$moduleId = $request->getPost('module_id');
		$moduleId = ($moduleId ==  'GLOBAL') ? null : $moduleId;
		$name = $request->getPost('name');
		$title = $request->getPost('title');
		$description = $request->getPost('description');
		
		$hookInfo = Gio_Core_Hook_Config::getHookInfo($name, $moduleId);
		$hook = array(
					'module' 		=> $moduleId,
					'name' 			=> $hookInfo['name'],
					'description'	=> $hookInfo['description'],
					'author'		=> $hookInfo['author'],
					'email'			=> $hookInfo['email'],
					'license'		=> $hookInfo['license'],
					'version'		=> $hookInfo['version'],
					'thumbnail'		=> $hookInfo['thumbnail']
				);
		if (Modules_Core_Services_Hook::exist($hook) == true) {
			return;
		}
		Modules_Core_Services_Hook::add($hook);
		$response = 'RESULT_OK';
		$this->getResponse()->setBody($response);
	}
	
	public function listAction()
	{
		$request = $this->getRequest();
		if ($request->isPost()) {
			$this->setNoRender();
			$this->disableLayout();
			
			$mod = $request->getPost('mod');
			$mod = ($mod == 'HOOK_GLOGAL') ? null : $mod;
			$hooks = Modules_Core_Services_Hook::getHooks($mod);
			$this->view->hooks = $hooks;
			
			$dbHooks = Modules_Core_Services_Hook::dbHooks($mod, true);
			$installedHooks = array();
			if ($dbHooks) {
				foreach ($dbHooks as $hook) {
					$installedHooks[] = $hook['module'] . '_' . $hook['name'];
				}
			}
			$this->view->installedHooks = $installedHooks;
			
			$viewFile = ROOT_DIR . DS . 'templates' . DS . $this->view->APP_TEMPLATE
							. DS . 'modules' . DS . 'core' . DS . 'hook' . DS . 'hook.ajax.phtml';
			$response = $this->view->render($viewFile);
			$this->getResponse()->setBody($response);
			return;
		}
		
		$modules = Modules_Core_Services_Module::getModulesInstalled();
		/**
		 * Add module core to the top array 
		 */
		$modules = array_pad($modules, -count($modules) - 1, array('module_id' => 'core'));
		$modules = array_pad($modules, -count($modules) - 1, array('module_id' => 'HOOK_GLOGAL'));
		$this->view->modules = $modules;
	}
	
	public function uninstallAction()
	{
		$this->setNoRender();
		$this->disableLayout();
		$response = 'RESULT_NOT_OK';
		
		$request = $this->getRequest();
		if (!$request->isPost()) {
			return;
		}
		
		$hookId = $request->getPost('hook_id');
		
		Modules_Core_Services_Hook::delete($hookId);
		$response = 'RESULT_OK';
		$this->getResponse()->setBody($response);
	}
	
//	public function ajaxAction()
//	{
//		$this->disableLayout()
//			 ->setNoRender();
//			 
//		$request = $this->getRequest();
//		if (!$request->isPost()) {
//			return;
//		}
//		
//		$module 	  = ucfirst(strtolower($request->getPost('mod')));
//		$name 		  = ucfirst(strtolower($request->getPost('name')));
//		$cacheEnable  = $request->getPost('cache');
//		$cacheEnable  = ($cacheEnable == 'true') ? true : false;
//		$cacheTimeout = $request->getPost('timeout');
//		$cacheTimeout = ($cacheTimeout > 0) ? $cacheTimeout : 3600;
//		
//		$params = array();
//		$paramString = $request->getPost('params');
//		if ($paramString) {
//			$params = explode('|', $paramString);
//			foreach ($params as $index => $param) {
//				$data = explode('=', $param);
//				if ($param && count($data) == 2) {
//					$request->setParam($data[0], $data[1]);
//				}
//			}
//		}
//		$action = $request->getPost('act', 'show');
//		
//		$response = Modules_Core_Services_Hook::load($module, $name, $action, $params, $cacheEnable, $cacheTimeout);
//		$json = new Services_JSON();
//		$this->getResponse()->setBody($json->encodeUnsafe($response));
//	}
}